/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.ams.service.impl;

import cn.tedu.mall.ams.exception.CoolSharkException;
import cn.tedu.mall.ams.mapper.PermissionMapper;
import cn.tedu.mall.ams.mapper.RolePermissionMapper;
import cn.tedu.mall.ams.service.IPermissionService;
import cn.tedu.mall.common.pojo.admin.dto.PermissionAddDTO;
import cn.tedu.mall.common.pojo.admin.dto.PermissionUpdateDTO;
import cn.tedu.mall.common.pojo.admin.model.Permission;
import cn.tedu.mall.common.pojo.admin.query.PermissionQuery;
import cn.tedu.mall.common.pojo.admin.vo.PermissionVO;
import cn.tedu.mall.common.restful.JsonPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @since 2021-12-02
 */
@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void addPermission(PermissionAddDTO permissionAddDTO) {
        //转化对象
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionAddDTO, permission);
        permissionMapper.insertPermission(permission);
    }

    @Override
    public JsonPage<PermissionVO> listPermissions(PermissionQuery permissionQuery) {
        PageHelper.startPage(permissionQuery.getPageNum(), permissionQuery.getSizeNum());
        List<PermissionVO> permissionVOList = permissionMapper.selectPermissions(permissionQuery);
        PageInfo pageInfo = new PageInfo(permissionVOList);
        return JsonPage.restPage(pageInfo);
    }

    @Override
    public void updatePermission(PermissionUpdateDTO permissionUpdateDTO) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionUpdateDTO, permission);
        permissionMapper.updatePermission(permission);
    }

    @Override
    public void deletePermission(Long id) {
        //检查是否有关联
        int count = rolePermissionMapper.selectExistByPermissionId(id);
        if (count != 0) {
            throw new CoolSharkException("当前权限有关联角色不可删除", 409);
        }
        permissionMapper.deletePermission(id);
    }
}
